﻿using System;
using System.Collections.Generic;
using System.Net;

namespace Toybox.SCM.Models.Accounts
{
	using LiveUtilities;
	using NicoSharp.Utilities;

	/// <summary>
	/// アカウント クラス
	/// </summary>
	public class Account : NicoSharp.Account
	{

		#region Constructor

		public Account()
			: base()
		{
		}

		public Account(params string[] elements)
			: this()
		{
			bool bResult = false;
			int iResult = 0;

			for (int n = 0; n < elements.Length; n++)
			{
				switch (n)
				{
					case 0:
						if (bool.TryParse(elements[n], out bResult))
							this.IsDefault = bResult;
						break;
					case 1:
						this.UId = elements[n];
						break;
					case 2:
						this.Name = elements[n];
						break;
					case 3:
						this.Id = elements[n];
						break;
					case 4:
						this.MailAddress = elements[n];
						break;
					case 5:
						this.Password = elements[n];
						break;
					case 6:
						if (bool.TryParse(elements[n], out bResult))
							this.Is184 = bResult;
						break;
					case 7:
						var counter = elements[n].Split(new string[] { "/" }, StringSplitOptions.None);
						if (int.TryParse(counter[0], out iResult))
							this.Counter.MaxCount = iResult;
						if (int.TryParse(counter[1], out iResult))
							this.Counter.Time = iResult;
						break;
					case 8:
						this.Cookies = new CookieCollection();
						this.Cookies.Add(new Cookie("user_session", elements[n], "/", ".nicovideo.jp"));
						break;
				}
			}
		}

		#endregion Constructor


		#region Public Members

		/// <summary>
		/// Unique ID
		/// </summary>
		public string UId
		{
			get { return this._uId; }
			set
			{
				if (this._uId == value) return;
				this._uId = value;
				base.OnPropertyChanged("UId");
			}
		}
		private string _uId;

		/// <summary>
		/// Default Flag
		/// </summary>
		public bool IsDefault
		{
			get { return this._isDefault; }
			set
			{
				if (this._isDefault == value) return;
				this._isDefault = value;
				base.OnPropertyChanged("IsDefault");
			}
		}
		private bool _isDefault;

		/// <summary>
		/// Anonymous Comment Flag
		/// </summary>
		public bool Is184
		{
			get { return this._is184; }
			set
			{
				if (this._is184 == value) return;
				this._is184 = value;
				base.OnPropertyChanged("Is184");
			}
		}
		private bool _is184;

		/// <summary>
		/// コメント数カウント
		/// </summary>
		public CommentCounter Counter
		{
			get { return this._counter ?? (this._counter = new CommentCounter()); }
			set
			{
				if (this._counter == value) return;
				this._counter = value;
				base.OnPropertyChanged("Counter");
			}
		}
		private CommentCounter _counter;

		// TODO:接続部分

		#endregion Public Members


		#region OverrideMethods

		/// <summary>
		/// 文字列化
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			var list = new List<string>();
			list.Add(this.IsDefault.ToString());
			list.Add(this.UId);
			list.Add(this.Id);
			list.Add(this.Name);
			list.Add(this.MailAddress);
			list.Add(this.Password);
			list.Add(this.Is184.ToString());
			list.Add(this.Counter.MaxCount + "/" + this.Counter.Time);

			if (this.Cookies != null)
			{
				var cookie = this.Cookies["user_session"];
				list.Add(cookie.Value);
			}

			return CsvUtility.Compose(list.ToArray()) + "\n";
		}

		#endregion OverrideMethods

	}
}
